From 8eaed5c62b7a5876c8b28a6410f2eb5fc54e3eb6 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Thu, 19 Dec 2019 09:44:18 -0700 Subject: [PATCH] hide magic time value from test mode decisions. (#443) --- defs.h | 1 + garmin_gpi.cc | 6 +++--- globals.cc | 3 +-- html.cc | 2 +- kml.cc | 2 +- main.cc | 5 ++--- osm.cc | 6 +++--- random.cc | 22 ++++++++++++---------- util.cc | 9 ++++++++- xcsv.cc | 9 +++------ 10 files changed, 35 insertions(+), 30 deletions(-) diff --git a/defs.h b/defs.h index 711cb3df6..fcec22b3f 100644 --- a/defs.h +++ b/defs.h @@ -1123,6 +1123,7 @@ char* strlower(char* src); signed int get_tz_offset(); time_t mklocaltime(struct tm* t); time_t mkgmtime(struct tm* t); +bool gpsbabel_testmode(); gpsbabel::DateTime current_time(); void dotnet_time_to_time_t(double dotnet, time_t* t, int* millisecs); signed int month_lookup(const char* m); diff --git a/garmin_gpi.cc b/garmin_gpi.cc index 6cc6b70a7..fb2c5ada5 100644 --- a/garmin_gpi.cc +++ b/garmin_gpi.cc @@ -1462,7 +1462,7 @@ garmin_gpi_rd_init(const QString& fname) static void garmin_gpi_wr_init(const QString& fname) { - if (gpi_timestamp != 0) { /* not the first gpi output session */ + if ((gpi_timestamp != 0) && !gpsbabel_testmode()) { /* not the first gpi output session */ time_t t = time(nullptr); if (t <= gpi_timestamp) { gpi_timestamp++; /* don't create files with same timestamp */ @@ -1470,7 +1470,7 @@ garmin_gpi_wr_init(const QString& fname) gpi_timestamp = t; } } else { - gpi_timestamp = gpsbabel_time; /* always ZERO during 'testo' */ + gpi_timestamp = gpsbabel_time; } fout = gbfopen_le(fname, "wb", MYNAME); @@ -1553,7 +1553,7 @@ garmin_gpi_wr_deinit() mkshort_del_handle(&short_h); gbfclose(fout); - if ((opt_sleep) && (gpi_timestamp != 0)) { /* don't sleep during 'testo' */ + if ((opt_sleep) && !gpsbabel_testmode()) { /* don't sleep during 'testo' */ int sleep = atoi(opt_sleep); if (sleep < 1) { sleep = 1; diff --git a/globals.cc b/globals.cc index c1e455c62..0f7ea8906 100644 --- a/globals.cc +++ b/globals.cc @@ -25,7 +25,6 @@ global_options global_opts; const char gpsbabel_version[] = VERSION; -time_t gpsbabel_now; /* gpsbabel startup-time; initialized in main.c with time() */ -time_t gpsbabel_time; /* gpsbabel startup-time; initialized in main.c with current_time(), ! ZERO within testo ! */ +time_t gpsbabel_time; /* gpsbabel startup-time; initialized in main.c with current_time(), ! fixed within testo ! */ posn_status tracking_status; diff --git a/html.cc b/html.cc index a9e3f052c..0f7e19b7c 100644 --- a/html.cc +++ b/html.cc @@ -253,7 +253,7 @@ data_write() // Don't write this line when running test suite. Actually, we should // probably not write this line at all... - if (ugetenv("GPSBABEL_FREEZE_TIME").isNull()) { + if (!gpsbabel_testmode()) { gbfprintf(file_out, " \n", gpsbabel_version); } gbfprintf(file_out, " GPSBabel HTML Output\n"); diff --git a/kml.cc b/kml.cc index c45a31855..08f147e67 100644 --- a/kml.cc +++ b/kml.cc @@ -2001,7 +2001,7 @@ static void kml_write() writer->writeTextElement(QStringLiteral("name"), QStringLiteral("GPS device")); } - if (current_time().isValid()) { + if (!gpsbabel_testmode()) { writer->writeTextElement(QStringLiteral("snippet"), QStringLiteral("Created ") + current_time().toString()); } diff --git a/main.cc b/main.cc index 5786b384c..586a80a0c 100644 --- a/main.cc +++ b/main.cc @@ -721,10 +721,9 @@ main(int argc, char* argv[]) global_opts.charset_name.clear(); global_opts.inifile = nullptr; - gpsbabel_now = time(nullptr); /* gpsbabel startup-time */ - gpsbabel_time = current_time().toTime_t(); /* same like gpsbabel_now, but frozen at zero during testo */ + gpsbabel_time = current_time().toTime_t(); /* frozen in testmode */ - if (gpsbabel_time != 0) { /* within testo ? */ + if (!gpsbabel_testmode()) { /* within testo ? */ global_opts.inifile = inifile_init(QString(), MYNAME); } diff --git a/osm.cc b/osm.cc index dd0f80ca9..4f4ae9060 100644 --- a/osm.cc +++ b/osm.cc @@ -837,7 +837,7 @@ osm_waypt_disp(const Waypoint* wpt) if (strlen(created_by) !=0) { gbfprintf(fout, " \n"); gbfprintf(fout, "\n"); diff --git a/random.cc b/random.cc index 057df2f5c..adc6c7dbd 100644 --- a/random.cc +++ b/random.cc @@ -24,6 +24,7 @@ #include // for QDateTime #include // for QString #include // for QThread +#include // for QVector #include "defs.h" #include "garmin_fs.h" // for garmin_fs_t, GMSD_SET, garmin_fs_flags_t, garmin_fs_alloc @@ -124,16 +125,22 @@ rand_qstr(const int maxlen, const char* fmt) } static void -random_rd_init(const QString&) +random_set_generator() { generator = new std::mt19937; if (opt_seed) { generator->seed(atoi(opt_seed)); } else { - generator->seed(gpsbabel_now); + generator->seed(gpsbabel_time); } } +static void +random_rd_init(const QString&) +{ + random_set_generator(); +} + static void random_rd_deinit() { @@ -243,7 +250,7 @@ random_read() route_head* head; Waypoint* prev = nullptr; - QDateTime time = QDateTime::fromTime_t(gpsbabel_time); + QDateTime time = current_time(); int points = (opt_points) ? atoi(opt_points) : rand_int(128) + 1; if (doing_trks || doing_rtes) { @@ -289,17 +296,12 @@ static realtime_data* realtime; void random_rd_posn_init(const QString&) { - generator = new std::mt19937; - if (opt_seed) { - generator->seed(atoi(opt_seed)); - } else { - generator->seed(gpsbabel_now); - } + random_set_generator(); realtime = new realtime_data; if (opt_points) { realtime->points = atoi(opt_points); } - realtime->time = QDateTime::fromTime_t(gpsbabel_time); + realtime->time = current_time(); } void diff --git a/util.cc b/util.cc index 2bc397a71..6eeaff238 100644 --- a/util.cc +++ b/util.cc @@ -715,6 +715,13 @@ mklocaltime(struct tm* t) return result; } +bool +gpsbabel_testmode() +{ + static bool testmode = getenv("GPSBABEL_FREEZE_TIME") != nullptr; + return testmode; +} + /* * Historically, when we were C, this was A wrapper for time(2) that * allowed us to "freeze" time for testing. The UNIX epoch @@ -725,7 +732,7 @@ mklocaltime(struct tm* t) gpsbabel::DateTime current_time() { - if (getenv("GPSBABEL_FREEZE_TIME")) { + if (gpsbabel_testmode()) { return QDateTime::fromTime_t(0); } diff --git a/xcsv.cc b/xcsv.cc index 84e605051..e6627c3ac 100644 --- a/xcsv.cc +++ b/xcsv.cc @@ -813,7 +813,7 @@ xcsv_parse_val(const char* s, Waypoint* wpt, const field_map& fmp, wpt->SetCreationTime(sscanftime(s, fmp.printfc.constData(), 1)); break; case XT_LOCAL_TIME: - if (ugetenv("GPSBABEL_FREEZE_TIME").isNull()) { + if (!gpsbabel_testmode()) { wpt->creation_time += sscanftime(s, fmp.printfc.constData(), 0); } else { /* Force constant time zone for test */ @@ -1763,13 +1763,10 @@ xcsv_replace_tokens(const QString& original) { // Don't do potentially expensive replacements if token prefix // isn't present; if (original.contains("__")) { - time_t my_time = gpsbabel_time; - replacement.replace("__FILE__", xcsv_file.fname); - replacement.replace("__VERSION__", my_time == 0 ? "" : gpsbabel_version); + replacement.replace("__VERSION__", gpsbabel_testmode()? "" : gpsbabel_version); - QDateTime dt = QDateTime::fromTime_t(my_time); - dt = dt.toTimeSpec(Qt::UTC); + QDateTime dt = current_time().toUTC(); QString dts = dt.toString("ddd MMM dd hh:mm:ss yyyy"); replacement.replace("__DATE_AND_TIME__", dts); -- 2.30.2